#import "Common.h"
#import "DataType.h"
#import "LayerOption.h"
#import "LayerMainMenu.h"
#import "DataSL.h"

NSString* sOptionTitle[4] = { @"OptionMusic", @"OptionSound", @"OptionSpellPos", @"OptionOpType" };
CGPoint sOptionItemPos[2] = { ccp(410,30), ccp(70,290) };
NSString* sOptionBtnLabel[2] = { @"BtnClose", @"BtnRate" };

@implementation SceneOption
-(id) init
{
	if ((self = [super init]))
		[self addChild:[LayerOption node]];
	return self;
}
@end

@implementation LayerOption

-(id) init
{
	if ((self = [super init])) {
		CCSprite* bg = [CCSprite spriteWithFile:@"background.png"];
		bg.position = gPointCenter;
		bg.opacity = 100;
		[self addChild:bg];
		
		// Title
		CCLabelTTF* title = [CCLabelTTF labelWithString:NSLocalizedString(@"OptionTitle",nil)
											   fontName:gFontName
											   fontSize:30];
		title.position = ccp(240,280);
		title.color = ccGREEN;
		[self addChild:title];
		
		CCMenu* menu = [CCMenu menuWithItems:NULL];
		menu.position = CGPointZero;
		[self addChild:menu];
		
		CCTextureCache* cache = [CCTextureCache sharedTextureCache];
		CCTexture2D* btnNormal = [cache addImage:@"btn-02.png"];
		CCTexture2D* btnNormalDown = [cache addImage:@"btn-02-s.png"];
		
		CCSprite *item, *itemDown;
		CCMenuItemSprite *menuItem, *itemOn, *itemOff;
		CCLabelTTF* labelTTF;

		for(int i=0; i<4; i++) {
			int y = 210-i*45;
			CCLabelTTF* itemName = [CCLabelTTF labelWithString:NSLocalizedString(sOptionTitle[i],nil)
													  fontName:gFontName
													  fontSize:20];
			itemName.position = ccp(140,y);
			[self addChild:itemName];

			item = [CCSprite spriteWithTexture:btnNormal];
			itemDown = [CCSprite spriteWithTexture:btnNormalDown];
			itemOn = [CCMenuItemSprite itemFromNormalSprite:item
											 selectedSprite:itemDown
													 target:self
												   selector:@selector(callMenu:)];
			if(i == 3)
				labelTTF = [CCLabelTTF labelWithString:NSLocalizedString(@"OptionRelative",nil)
											  fontName:gFontName
											  fontSize:16];
			else if( i == 2)
				labelTTF = [CCLabelTTF labelWithString:NSLocalizedString(@"OptionLeft",nil)
											  fontName:gFontName
											  fontSize:16];
			else
				labelTTF = [CCLabelTTF labelWithString:NSLocalizedString(@"OptionOn",nil)
											  fontName:gFontName
											  fontSize:16];
			labelTTF.position = ccp(item.contentSize.width/2,item.contentSize.height/2+2);
			labelTTF.color = ccYELLOW;
			[itemOn addChild:labelTTF];

			item = [CCSprite spriteWithTexture:btnNormal];
			itemDown = [CCSprite spriteWithTexture:btnNormalDown];
			itemOff = [CCMenuItemSprite itemFromNormalSprite:item
											  selectedSprite:itemDown
													  target:self
													selector:@selector(callMenu:)];
			if(i == 3)
				labelTTF = [CCLabelTTF labelWithString:NSLocalizedString(@"OptionAbsolute",nil)
											  fontName:gFontName
											  fontSize:16];
			else if( i == 2)
				labelTTF = [CCLabelTTF labelWithString:NSLocalizedString(@"OptionRight",nil)
											  fontName:gFontName
											  fontSize:16];
			else
				labelTTF = [CCLabelTTF labelWithString:NSLocalizedString(@"OptionOff",nil)
											  fontName:gFontName
											  fontSize:16];
			labelTTF.position = ccp(item.contentSize.width/2,item.contentSize.height/2+2);
			labelTTF.color = ccYELLOW;
			[itemOff addChild:labelTTF];

			mItemToggle[i] = [CCMenuItemToggle itemWithTarget:self
													 selector:@selector(callMenu:)
														items:itemOn,itemOff,NULL];
			mItemToggle[i].position = ccp(300,y);
			[menu addChild:mItemToggle[i] z:1000+i];
		}
		
		for(int i=0; i<2; i++) {
			item = [CCSprite spriteWithTexture:btnNormal];
			itemDown = [CCSprite spriteWithTexture:btnNormalDown];
			menuItem = [CCMenuItemSprite itemFromNormalSprite:item
											   selectedSprite:itemDown
													   target:self
													 selector:@selector(callMenu:)];
			menuItem.position = sOptionItemPos[i];
			labelTTF = [CCLabelTTF labelWithString:NSLocalizedString(sOptionBtnLabel[i],nil)
										  fontName:gFontName
										  fontSize:16];
			labelTTF.position = ccp(item.contentSize.width/2,item.contentSize.height/2+2);
			[menuItem addChild:labelTTF];
			[menu addChild:menuItem z:i+100];
		}
		
		[mItemToggle[0] setSelectedIndex:!gSettingData.musicOn];
		[mItemToggle[1] setSelectedIndex:!gSettingData.soundOn];
		[mItemToggle[2] setSelectedIndex:gSettingData.spellInRight];
		[mItemToggle[3] setSelectedIndex:gSettingData.operationType];
	}
	return self;
}

-(void) callMenu:(CCMenuItem*)sender
{
	int id = sender.zOrder;

	[[SimpleAudioEngine sharedEngine] playEffect:SOUND_BTN01];
	
	switch(id) {
		case 1000:		// Music
			[[SimpleAudioEngine sharedEngine] setBackgroundMusicVolume:([mItemToggle[0] selectedIndex] == 0) ? 0.8 : 0];
			break;
		case 1001:		// Sound
			[[SimpleAudioEngine sharedEngine] setEffectsVolume:([mItemToggle[1] selectedIndex] == 0) ? 1 : 0];
			break;
		case 100:		// Back
			gSettingData.musicOn = ([mItemToggle[0] selectedIndex] == 0);
			gSettingData.soundOn = ([mItemToggle[1] selectedIndex] == 0);
			gSettingData.spellInRight = ([mItemToggle[2] selectedIndex] == 1);
			gSettingData.operationType = [mItemToggle[3] selectedIndex];
			gSettingData.startPosX = gSettingData.spellInRight ? 0 : 50;
			
			gSaveData();

			[[CCDirector sharedDirector] replaceScene:
			 [CCTransitionFade transitionWithDuration:0.5 scene:[SceneMainMenu node]]];
			break;
		case 101:		// Rate
		{
		   gGlobalData.reviewTimes = -1;
		   gSaveData();
		   NSString* str = @"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=461205872&onlyLatestVersion=false&type=Purple+Software&sortOrding=1&pageNumber=0";
		   [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
//			UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"BtnRate",nil)
//															message:NSLocalizedString(@"RateMsg",nil)
//														   delegate:self
//												  cancelButtonTitle:NSLocalizedString(@"BtnCancel",nil)
//												  otherButtonTitles:NSLocalizedString(@"BtnGoto",nil), nil];
//			[alert show];
//			[alert release];
			break;
		}
	}
}


-(void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if(buttonIndex == 1) {
		// Rate it
		NSString* str = @"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=461205872&onlyLatestVersion=false&type=Purple+Software&sortOrding=1&pageNumber=0";
		[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
	}
}

@end